package com.cinc.ebc.sd.service.impl;

import com.cinc.ebc.sd.entity.SdSalesReturn;
import com.cinc.ebc.sd.entity.SdSalesReturnDetail;
import com.cinc.ebc.sd.mapper.SdSalesReturnDetailMapper;
import com.cinc.ebc.sd.mapper.SdSalesReturnMapper;
import com.cinc.ebc.sd.service.ISdSalesReturnService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 销售退货单
 * @Author: jeecg-boot
 * @Date:   2022-10-09
 * @Version: V1.0
 */
@Service
public class SdSalesReturnServiceImpl extends ServiceImpl<SdSalesReturnMapper, SdSalesReturn> implements ISdSalesReturnService {

	@Autowired
	private SdSalesReturnMapper sdSalesReturnMapper;
	@Autowired
	private SdSalesReturnDetailMapper sdSalesReturnDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(SdSalesReturn sdSalesReturn, List<SdSalesReturnDetail> sdSalesReturnDetailList) {
		sdSalesReturnMapper.insert(sdSalesReturn);
		if(sdSalesReturnDetailList!=null && sdSalesReturnDetailList.size()>0) {
			for(SdSalesReturnDetail entity:sdSalesReturnDetailList) {
				//外键设置
				entity.setMainId(sdSalesReturn.getId());
				sdSalesReturnDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(SdSalesReturn sdSalesReturn,List<SdSalesReturnDetail> sdSalesReturnDetailList) {
		sdSalesReturnMapper.updateById(sdSalesReturn);
		
		//1.先删除子表数据
		sdSalesReturnDetailMapper.deleteByMainId(sdSalesReturn.getId());
		
		//2.子表数据重新插入
		if(sdSalesReturnDetailList!=null && sdSalesReturnDetailList.size()>0) {
			for(SdSalesReturnDetail entity:sdSalesReturnDetailList) {
				//外键设置
				entity.setMainId(sdSalesReturn.getId());
				sdSalesReturnDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		sdSalesReturnDetailMapper.deleteByMainId(id);
		sdSalesReturnMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			sdSalesReturnDetailMapper.deleteByMainId(id.toString());
			sdSalesReturnMapper.deleteById(id);
		}
	}
	
}
